<?

    if (isset($_GET['q']))
    {
        $r = $bibtex->search($_GET['q']);
        $smarty->assign('search_query', $r[2]);        
        $found = $r[0];
        
        $query = '';
        if ($r[1][0] == '')
        {
            $query = '"'.$r[1][1].'"';
        }
        else
        {
            $query = '"'.$r[1][1].'" (Restricted to '.$r[1][0].')';
        }
        
        if (count($found) == 1)
        {
            refreshPage('view', array('ID'=>$found[0]['ID']));
        }
        
        foreach($found as $k=>$v)
        {
                $found[$k]['error'] = '';
                $found[$k]['author'] = explode(' and', $v['author']);
                $found[$k]['user'] = $user->getFullname($v['user']);

                /******************
                 CREATE PNG PREVIEW
                 ******************/
                $preview = $bibtex->getPreview($v['ID']);
                $found[$k]['preview'] = '';
                if ($preview !== false)
                {
                    $found[$k]['preview'] = $preview;
                }

            $found[$k]['error'] = array();
            if ($v['suppressWarnings'] == 0)
            {
                foreach($found[$k]['author'] as $a)
                {
                    $names = explode(',', $a);
                    $names[1] = trim($names[1]);
                    if ((strpos($names[1], '.') !== false && strpos($names[1], ' ') === false)|| 
                        (strpos($names[1], ' ') === false && strlen($names[1]) <= 2))
                    {
                        $found[$k]['error'][] = 'Check Name';
                    }
                }
                if ($found[$k]['pages'] != '' && strpos($found[$k]['pages'], '--') === false)
                {
                    $found[$k]['error'][] = 'Check Pages';
                }
            }
        }
        $smarty->assign('results', $found);        
        $smarty->assign('searchQuery', stripslashes($query));
    }

?>
